const app = getApp()
Page({
  data: {
    ktmc: '',
    ktjj: '',
    jsyq: '',
    ckzl: '',
    loading: false,
    disabled: false
  },
  inpKtmc: function(e) {
    this.setData({
      ktmc: e.detail.value
    })
  },
  inpKtjj: function(e) {
    this.setData({
      ktjj: e.detail.value
    })
  },
  inpJsyq: function(e) {
    this.setData({
      jsyq: e.detail.value
    })
  },
  inpCkzl: function(e) {
    this.setData({
      ckzl: e.detail.value
    })
  },
  funFb: function() {
    const that = this
    that.setData({
      loading: true,
      disabled: true
    })
    if (that.data.ktmc == '') {
      wx.showToast({
        title: '请输入课题名称！',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        loading: false,
        disabled: false
      })
    } else if (that.data.ktjj == '') {
      wx.showToast({
        title: '请输入课题简介！',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        loading: false,
        disabled: false
      })
    } else if (that.data.jsyq == '') {
      wx.showToast({
        title: '请输入课题技术要求！',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        loading: false,
        disabled: false
      })
    } else if (that.data.ckzl == '') {
      wx.showToast({
        title: '请输入课题参考资料！',
        icon: 'none',
        duration: 2000
      })
      that.setData({
        loading: false,
        disabled: false
      })
    } else {
      wx.cloud.callFunction({
        name: 'topicRelease',
        data: {
          ktmc: that.data.ktmc,
          ktjj: that.data.ktjj,
          jsyq: that.data.jsyq,
          ckzl: that.data.ckzl,
          dept: app.globalData.userDept,
          teaName: app.globalData.userName,
          teaId: app.globalData.userId
        }
      }).then(res => {
        if (res.result.result == 1) {
          wx.showToast({
            title: '课题发布成功！',
            icon: 'success',
            duration: 2000
          })
          that.setData({
            loading: false
          })
          setTimeout(function() {
            that.setData({
              disabled: false
            })
          }, 2500)
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            })
          }, 2500)
        } else {
          wx.showToast({
            title: '课题发布失败！',
            icon: 'none',
            duration: 2000
          })
          that.setData({
            loading: false,
            disabled: false
          })
        }
      })
    }
  }
})